using UnityEngine;

namespace AudioStudio
{
    [AddComponentMenu("AudioStudio/EmitterSound")]
    public class EmitterSound : AudioEventHandler
    {
        public System.Collections.Generic.List<UnityEngine.Vector3> multiPositionArray =
        new System.Collections.Generic.List<UnityEngine.Vector3>();

        public AudioEvent audioevent;
        public AkMultiPositionType positiontype = AkMultiPositionType.MultiPositionType_MultiDirections;

        new void Awake()
        {
            this.StopOnDestroy = false;
            base.Awake();
        }

        public override void HandleEnableEvent()
        {
            if (AkSoundEngine.IsInitialized())
            {
                var positionArray = BuildAkPositionArray();
                AkSoundEngine.SetMultiplePositions(this.Player, positionArray, (ushort)positionArray.Count, positiontype);
                if (audioevent.Id != 0)
                    audioevent.Post(this.Player);
            }
        }

        public override void HandleDisableEvent()
        {
            
        }

        private AkPositionArray BuildAkPositionArray()
        {
            var positionArray = new AkPositionArray((uint)multiPositionArray.Count);
            for (var i = 0; i < multiPositionArray.Count; i++)
                positionArray.Add(transform.position + multiPositionArray[i], transform.forward, transform.up);

            return positionArray;
        }
    }
}